﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Controllers.BoMonService;
using eTMS.Controllers.MonHocService;

namespace eTMS.UserControls
{
    public partial class ChonMonHoc : UserControl
    {
        public ChonMonHoc()
        {
            InitializeComponent();
        }
        private int _HocKy_ID = 0;
        private int _NamHoc_ID = 0;
        public int HocKy_ID
        {
            set
            {
                _HocKy_ID = value;
            }
        }
        public int NamHoc_ID
        {
            set
            {
                _NamHoc_ID = value;
            }
        }
        public void SetDefaultKhoa_ID(int Khoa_ID)
        {
            comboBoxKhoa1.SelectedValue = Khoa_ID;
        }
        private List<int> _LsMonHocHidden = new List<int>();
        private MonHocController monHocController;
        public void SetHiddenList(List<int> lsMonHoc_ID)
        {
            _LsMonHocHidden = lsMonHoc_ID;
        }
        private void comboBoxKhoa1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (DesignMode) return;
            comboBoxBoMon1.AddSearchValues("Khoa_ID", comboBoxKhoa1.SelectedValue);
            comboBoxBoMon1.BindData();
        }

        private void comboBoxBoMon1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (DesignMode) return;
            List<int> ls = new List<int>();
            if (comboBoxBoMon1.SelectedIndex < 0) return;
            if (comboBoxBoMon1.SelectedIndex == 0)
            {
                foreach (BoMonEntity bomonEntity in comboBoxBoMon1.Items)
                {
                    ls.Add(bomonEntity.BoMon_ID);
                }
            }
            else
            {
                ls.Add((int)comboBoxBoMon1.SelectedValue);
            }
            if (monHocController == null) monHocController = new MonHocController();
            monHocController.LoadMonHocByListBoMon(ls);
            if (_LsMonHocHidden.Count > 0)
            {
                monHocController.Table.DefaultView.RowFilter = "MonHoc_ID NOT IN(" + string.Join(",", _LsMonHocHidden.ConvertAll<string>(Convert.ToString).ToArray()) + ")";
                monHocController.Table = monHocController.Table.DefaultView.ToTable();
                monHocController.ResetTableIndex();
            }
            monHocCheckList1.UpdateToScreen();
        }



        public List<MonHocEntity> GetCheckedMonHoc()
        {
            List<MonHocEntity> ls = new List<MonHocEntity>();
            foreach (DataRow row in monHocController.Table.Rows)
            {
                if (monHocController.ConvertBool(row["Checked"]))
                {
                    ls.Add(monHocController.MapDataRowToObject(row));
                }
            }
            return ls;
        }

        private void ChonMonHoc_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            KhoaController.LoadAllKhoa(false);
            BoMonController.LoadAllBoMon(false);
            if (monHocController == null) monHocController = new MonHocController();
            monHocCheckList1.SetController(monHocController);
            comboBoxKhoa1.BindData();
           
        }
    }
}